<?php

    session_start();

    include("function-access.php");
    $conn = connectSSO();
    
    $data_array = array();
    $sqlNotif = "SELECT a.id_notif, c.name_sim, d.username, d.pid, d.user_type, a.header, CONCAT(c.url,'',a.url) AS url, b.status, a.tanggal FROM tb_notif a
                        LEFT JOIN tb_notif_user b
                        ON b.id_notif = a.id_notif
                        LEFT JOIN tb_sim c
                        ON c.id_sim = a.id_sim
                        LEFT JOIN tb_user d
                        ON d.id_user = a.notifier
                        WHERE b.id_user =  '".$_SESSION["ses_idUser"]."'  AND b.id_role = '".$_SESSION["ses_idRole"]."'";
    //echo $sqlNotifBaru;
    
    switch ($_GET["act"]) {
        case 1:
            $sqlNotif .= " ORDER BY b.status ASC, a.tanggal DESC LIMIT 5"; 
            // echo $sqlNotif;
            $queryNotifBaru = mysqli_query($conn ,$sqlNotif);
            while($baris_list_pesan = mysqli_fetch_array($queryNotifBaru)){
                $row_array["idNotif"] = $baris_list_pesan["id_notif"];
                $row_array["sim"] = $baris_list_pesan["name_sim"];
                $row_array["sender"] = $baris_list_pesan["username"];
                $row_array["subjek"] = $baris_list_pesan["header"];
                $row_array["url"] = $baris_list_pesan["url"];
                $row_array["gambar"] = pictURL($baris_list_pesan["pid"], $baris_list_pesan["user_type"], 3);
                $row_array["tgl"] = dateTime($baris_list_pesan["tanggal"]);
                $row_array["statusNotif"] = $baris_list_pesan["status"];
                array_push($data_array,$row_array);
            }
            break;
        case 2:
            $sqlNotif .= " AND b.status = '0'";
            $queryNotifBaru = mysqli_query($conn ,$sqlNotif);
            $banyakNotifBaru = mysqli_num_rows($queryNotifBaru);
            
            $row_array["banyakNotif"] = $banyakNotifBaru;
            array_push($data_array,$row_array);
            break;
        case 3:
            $sqlNotif .= " AND b.status = '0' ORDER BY a.tanggal";
            // echo $sqlNotif;
            $queryNotifBaru = mysqli_query($conn ,$sqlNotif);
            while($baris_list_pesan = mysqli_fetch_array($queryNotifBaru)){
                $row_array["idNotif"] = $baris_list_pesan["id_notif"];
                $row_array["sim"] = $baris_list_pesan["name_sim"];
                $row_array["sender"] = $baris_list_pesan["username"];
                $row_array["subjek"] = $baris_list_pesan["header"];
                $row_array["url"] = $baris_list_pesan["url"];
                $row_array["gambar"] = pictURL($baris_list_pesan["pid"], $baris_list_pesan["user_type"], 3);
                $row_array["tgl"] = dateTime($baris_list_pesan["tanggal"]);
                $row_array["statusNotif"] = $baris_list_pesan["status"];
                array_push($data_array,$row_array);
            }
            break;
    }
    
    echo json_encode($data_array);

?>